package com.dayu.web.controller.recovery;

import java.util.List;

import com.dayu.common.annotation.RepeatSubmit;
import com.dayu.recovery.domain.RecoveryGoods;
import com.dayu.recovery.service.IRecoveryGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dayu.common.annotation.Log;
import com.dayu.common.core.controller.BaseController;
import com.dayu.common.core.domain.AjaxResult;
import com.dayu.common.enums.BusinessType;
import com.dayu.recovery.domain.RecoveryLoan;
import com.dayu.recovery.service.IRecoveryLoanService;
import com.dayu.common.utils.poi.ExcelUtil;
import com.dayu.common.core.page.TableDataInfo;

/**
 * 暂借登记Controller
 *
 * @author HK
 * @date 2021-04-16
 */
@RestController
@RequestMapping("/recovery/loan")
public class RecoveryLoanController extends BaseController
{
    @Autowired
    private IRecoveryLoanService recoveryLoanService;
    @Autowired
    private IRecoveryGoodsService goodsService;
    /**
     * 查询暂借登记列表
     */
    @PreAuthorize("@ss.hasPermi('recovery:loan:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecoveryLoan recoveryLoan)
    {
        startPage();
        List<RecoveryLoan> list = recoveryLoanService.selectRecoveryLoanList(recoveryLoan);
        return getDataTable(list);
    }

    /**
     * 导出暂借登记列表
     */
    @PreAuthorize("@ss.hasPermi('recovery:loan:export')")
    @Log(title = "暂借登记", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RecoveryLoan recoveryLoan)
    {
        List<RecoveryLoan> list = recoveryLoanService.selectRecoveryLoanList(recoveryLoan);
        ExcelUtil<RecoveryLoan> util = new ExcelUtil<RecoveryLoan>(RecoveryLoan.class);
        return util.exportExcel(list, "loan");
    }

    /**
     * 获取暂借登记详细信息
     */
    @PreAuthorize("@ss.hasPermi('recovery:loan:query')")
    @GetMapping(value = "/{loanId}")
    public AjaxResult getInfo(@PathVariable("loanId") Long loanId)
    {
        return AjaxResult.success(recoveryLoanService.selectRecoveryLoanById(loanId));
    }

    /**
     * 新增暂借登记
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('recovery:loan:add')")
    @Log(title = "暂借登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecoveryLoan recoveryLoan)
    {
        return toAjax(recoveryLoanService.insertRecoveryLoan(recoveryLoan));
    }

    /**
     * 修改暂借登记
     */
    @PreAuthorize("@ss.hasPermi('recovery:loan:edit')")
    @Log(title = "暂借登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecoveryLoan recoveryLoan)
    {
        return toAjax(recoveryLoanService.updateRecoveryLoan(recoveryLoan));
    }

    /**
     * 删除暂借登记
     */
    @PreAuthorize("@ss.hasPermi('recovery:loan:remove')")
    @Log(title = "暂借登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{loanIds}")
    public AjaxResult remove(@PathVariable Long[] loanIds)
    {
        return toAjax(recoveryLoanService.deleteRecoveryLoanByIds(loanIds));
    }

    /**
     * 查询商品信息
     */
    @PreAuthorize("@ss.hasPermi('recovery:loan:add')")
    @PostMapping("/get/goods")
    public AjaxResult getGoods(@RequestBody RecoveryGoods recoveryGoods) {
        List<RecoveryGoods> goodsList = goodsService.selectRecoveryGoodsList(recoveryGoods);
        if(goodsList == null || goodsList.isEmpty()) {
            return AjaxResult.error("没有查到数据！");
        }
        RecoveryGoods goods = goodsList.get(0);

        AjaxResult res = goods.goodsVerify();
        if(res != null) {
            return res;
        }
        return AjaxResult.success(goods);
    }

    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('recovery:loan:add')")
    @Log(title = "暂借登记批量新增", businessType = BusinessType.INSERT)
    @PostMapping("/add/list")
    public AjaxResult addList(@RequestBody List<RecoveryLoan> list) {
        return toAjax(recoveryLoanService.addLoanList(list));
    }
}
